//-----------------------------------------------------------------------------
//File: Core_System.cpp
//
//Create by : mouse (July 2012)
//-----------------------------------------------------------------------------

#include "Core_Global.h"

cApplication::cApplication()
{
	g_pApplication = this;
	m_hInst = ( HINSTANCE )GetModuleHandle( NULL );
	m_hMenu = NULL;

	wcscpy_s(m_Class, L"AppClass");
	wcscpy_s(m_Caption, L"Application Caption");

	m_Style  = WS_OVERLAPPEDWINDOW;
	m_XPos   = 100;
	m_YPos   = 100;
	m_Width  = 256;
	m_Height = 256;
	
	m_wcex.cbSize        = sizeof(WNDCLASSEX);
	m_wcex.style         = CS_CLASSDC;
	m_wcex.lpfnWndProc   = AppWindowProc;
	m_wcex.cbClsExtra    = 0;
	m_wcex.cbWndExtra    = 0;
	m_wcex.hInstance     = m_hInst;
	m_wcex.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
	m_wcex.hCursor       = LoadCursor(NULL, IDC_ARROW);
	m_wcex.hbrBackground = ( HBRUSH )GetStockObject( BLACK_BRUSH );
	m_wcex.lpszMenuName  = NULL;
	m_wcex.lpszClassName = m_Class;
	m_wcex.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
}

cApplication::~cApplication()
{
}

BOOL cApplication::Run()
{
	MSG msg;

	if(!RegisterClassEx(&m_wcex))
		return FALSE;
	
	// if menu 
	RECT rc;
	SetRect( &rc, 0, 0, m_Width, m_Height );
	AdjustWindowRect( &rc, WS_OVERLAPPEDWINDOW, ( m_hMenu != NULL ) ? true : false );

	m_hWnd = CreateWindow(m_Class,
					m_Caption,
					m_Style,
					m_XPos,
					m_YPos,
					( rc.right - rc.left ),
					( rc.bottom - rc.top ),
					NULL,
					m_hMenu,
					m_hInst, NULL);
	if(!m_hWnd)
		return FALSE;

	ShowWindow(m_hWnd, SW_NORMAL);
	UpdateWindow(m_hWnd);
	
	// Initialize COM
	CoInitialize(NULL);
	
	if(InitGame() == TRUE)
	{
		msg.message = WM_NULL;
		PeekMessage( &msg, NULL, 0U, 0U, PM_NOREMOVE );
		while(msg.message != WM_QUIT)
		{
			if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
			{
				TranslateMessage(&msg);
				DispatchMessage(&msg);
			}
			else
			{
				if(Frame() == FALSE)
					break;
			}
		}
	}

	Shutdown();
	
	// Shutdown COM
	CoUninitialize();
	
	// Unregister the window class
	UnregisterClass(m_Class, m_hInst);
	
	return TRUE;
}

BOOL cApplication::Error(BOOL Fatal, WCHAR *Text, ...)
{
  WCHAR CaptionText[12];
  WCHAR ErrorText[2048];
  va_list valist;

  // Build the message box caption based on fatal flag
  if(Fatal == FALSE)
    wcscpy_s(CaptionText, L"Error");
  else 
    wcscpy_s(CaptionText, L"Fatal Error");

  // Build variable text buffer
  va_start(valist, Text);
  vswprintf_s(ErrorText, Text, valist);
  va_end(valist);

  // Display the message box
  MessageBox(NULL, ErrorText, CaptionText, MB_OK | MB_ICONEXCLAMATION);

  // Post a quit message if error was fatal
  if(Fatal == TRUE)
    PostQuitMessage(0);

  return TRUE;
}

LRESULT cApplication::MsgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	return DefWindowProc( hWnd, uMsg, wParam, lParam );
}

LRESULT CALLBACK AppWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
	switch(uMsg)
	{
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
	
	default:
		if( g_pApplication )
			return g_pApplication->MsgProc( hWnd, uMsg, wParam, lParam );
	}
	return S_OK;
}